/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.bsscheduler;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;
import java.util.Vector;

/**
 *
 * @author marc
 */
public class EventsDisplay {

    private EventList events;
    private String username;
    private TreeMap<String, String> userList = UsersDB.userList();


    public void setEventList(EventList events){
        this.events = events;
    }

    public void setUserName(String name) {
        username = name;
    }

    public String getAllEvents(){
        String output = "";

        for(Event e : events.getAllEvents()){
            output += "<p>" + e.getStartDate() + "&nbsp;" + e.getTitle() + "</p>\n";
        }
 
        return output;
    }

    public String getUpcomingEvents(int maxNumOfEvents) {
        String output = "";

        if (maxNumOfEvents < 1 || maxNumOfEvents > 50) maxNumOfEvents = 10;
        int numOfEvents = 0;

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(now);

        output += "<table>\n";

        for(Event e : events.getAllEvents()) {

            if(!e.getParticipantStatus(username).equals("confirmed") &&
                    !e.getParticipantStatus(username).equals("creator")) {
                continue;
            }

            if (e.getRawStartDate().after(now)) {

                Vector<String> participants = new Vector<String>();
                participants.add(e.getCreator());
                participants.addAll(e.getParticipants());

                String participantArray = "[";
                for (String p: participants) {
                    String fullName = userList.get(p);
                    String status = e.getParticipantStatus(p);
                    participantArray += "[\\'" + fullName + "\\', \\'" +
                            status + "\\'],";
                }
                // Remove last comma
                participantArray = participantArray.substring(0,
                        participantArray.length() - 1);
                participantArray += "]";

                output += "<tr class=\"upcoming_appt\" onclick=\"" +
                                    "showModifyDialog('" +
                                    e.getUUID() + "', '" +
                                    e.getTitleEncoded() + "', '" +
                                    e.getDescriptionEncoded() + "', '" +
                                    e.getStartDate("yyyy-MM-dd") + "', '" +
                                    e.getStartDate("H:mm") + "', '" +
                                    e.getEndDate("yyyy-MM-dd") + "', '" +
                                    e.getEndDate("H:mm") + "', '" +
                                    participantArray + "')\")>";


                String eventDay = e.getStartDate("yyyy-MM-dd");
                String eventDateFormat = "E, MMM d";

                // If event is today, just print event time, not date
                if (today.equals(eventDay)) {
                    eventDateFormat = "h:mm a";
                }

                output += "    <td class=\"appt_time\">" +
                            e.getStartDate(eventDateFormat) + "</td>\n";
                output += "    <td class=\"appt_title\">" +
                            e.getTitle() + "</td>\n";

                output += "  </tr>\n";
                numOfEvents++;
            }
            if (numOfEvents == maxNumOfEvents) break;
        }

        if (numOfEvents == 0) {
            output += "  <tr>\n" +
                      "    <td class=\"no_appts\">You have no upcoming appointments.</td>\n" +
                      "  </tr>\n";
        }

        output += "</table>\n";

        return output;
    }

    public String getPendingEvents() {

        String output = "";

        int numOfEvents = 0;

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(now);

        output += "<table>\n";

        for(Event e : events.getPendingEventInvites()) {
            if (e.getRawStartDate().after(now)) {

                Vector<String> participants = new Vector<String>();
                participants.add(e.getCreator());
                participants.addAll(e.getParticipants());

                String participantArray = "[";
                for (String p: participants) {
                    String fullName = userList.get(p);
                    String status = e.getParticipantStatus(p);
                    participantArray += "[\\'" + fullName + "\\', \\'" +
                            status + "\\'],";
                }
                // Remove last comma
                participantArray = participantArray.substring(0,
                        participantArray.length() - 1);
                participantArray += "]";

                output += "  <tr class=\"upcoming_appt\">";
                output += "    <div onclick=\"" +
                                    "showModifyDialog('" +
                                    e.getUUID() + "', '" +
                                    e.getTitleEncoded() + "', '" +
                                    e.getDescriptionEncoded() + "', '" +
                                    e.getStartDate("yyyy-MM-dd") + "', '" +
                                    e.getStartDate("H:mm") + "', '" +
                                    e.getEndDate("yyyy-MM-dd") + "', '" +
                                    e.getEndDate("H:mm") + "', '" +
                                    participantArray + "')\")>";

                String eventDay = e.getStartDate("yyyy-MM-dd");
                String eventDateFormat = "E, MMM d";

                // If event is today, just print event time, not date
                if (today.equals(eventDay)) {
                    eventDateFormat = "h:mm a";
                }

                output += "    <td class=\"appt_time\">" +
                            e.getStartDate(eventDateFormat) + "</td>\n";
                output += "    <td class=\"appt_title\">" +
                            e.getTitle() + "</td>\n";

                output += "    </div>";

                output += "    <form action=\"updateevent.jsp\" method=\"post\">\n" +
                          "      <input type=\"hidden\" id=\"uuid\" name=\"uuid\" " +
                          "value=\"" + e.getUUID() + "\"/>\n";

                output += "    <td class=\"appt_confirm\"><input type=\"submit\" " +
                          "name=\"confirm\" value=\"confirm\"/></td>\n" +
                          "    <td class=\"appt_decline\"><input type=\"submit\" " +
                          "name=\"decline\" value=\"decline\"/></td>\n";

                output += "    </form>\n";
                output += "  </tr>\n";
                
                numOfEvents++;
            }
        }

        if (numOfEvents == 0) {
            output += "  <tr>\n" +
                      "    <td class=\"no_appts\">You have no pending appointment invites.</td>\n" +
                      "  </tr>\n";
        }

        output += "</table>\n";


        return output;
    }

}
